import 'dart:convert';

/// 安全的类型转换辅助函数

/// 将动态类型转换为int，支持String、int、double等
int _toInt(dynamic value, {int defaultValue = 0}) {
  if (value == null) return defaultValue;
  if (value is int) return value;
  if (value is double) return value.toInt();
  if (value is String) {
    final parsed = int.tryParse(value);
    return parsed ?? defaultValue;
  }
  return defaultValue;
}

/// 将动态类型转换为String
String _toString(dynamic value, {String defaultValue = ''}) {
  if (value == null) return defaultValue;
  if (value is String) return value;
  return value.toString();
}

/// 将动态类型转换为double
double _toDouble(dynamic value, {double defaultValue = 0.0}) {
  if (value == null) return defaultValue;
  if (value is double) return value;
  if (value is int) return value.toDouble();
  if (value is String) {
    final parsed = double.tryParse(value);
    return parsed ?? defaultValue;
  }
  return defaultValue;
}

/// 文章续写信息
class NotesContinuation {
  /// 上一集文章信息
  PreviousNotesInfo? previousNotes;

  /// 下一集文章列表
  List<NextNotesInfo> nextNotesList;

  NotesContinuation({
    this.previousNotes,
    required this.nextNotesList,
  });

  factory NotesContinuation.fromJson(Map<String, dynamic> json) {
    return NotesContinuation(
      previousNotes: json['previousNotes'] != null
          ? PreviousNotesInfo.fromJson(json['previousNotes'])
          : null,
      nextNotesList: (json['nextNotesList'] as List<dynamic>?)
              ?.map((e) => NextNotesInfo.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  factory NotesContinuation.fromJsonString(String jsonString) {
    return NotesContinuation.fromJson(jsonDecode(jsonString));
  }

  Map<String, dynamic> toJson() {
    return {
      'previousNotes': previousNotes?.toJson(),
      'nextNotesList': nextNotesList.map((e) => e.toJson()).toList(),
    };
  }

  String toJsonString() {
    return jsonEncode(toJson());
  }
}

/// 上一集文章信息
class PreviousNotesInfo {
  /// 文章ID
  int notesId;

  /// 文章标题
  String title;

  /// 封面图片
  String coverPicture;

  /// 点赞数
  int notesLikeNum;

  /// 收藏数
  int notesCollectionNum;

  /// 文章类型（0=图文，1=视频）
  int notesType;

  PreviousNotesInfo({
    required this.notesId,
    required this.title,
    required this.coverPicture,
    required this.notesLikeNum,
    required this.notesCollectionNum,
    this.notesType = 0, // 默认为图文类型
  });

  factory PreviousNotesInfo.fromJson(Map<String, dynamic> json) {
    return PreviousNotesInfo(
      notesId: _toInt(json['notesId']),
      title: _toString(json['title']),
      coverPicture: _toString(json['coverPicture']),
      notesLikeNum: _toInt(json['notesLikeNum']),
      notesCollectionNum: _toInt(json['notesCollectionNum']),
      notesType: _toInt(json['notesType']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'notesId': notesId,
      'title': title,
      'coverPicture': coverPicture,
      'notesLikeNum': notesLikeNum,
      'notesCollectionNum': notesCollectionNum,
      'notesType': notesType,
    };
  }
}

/// 下一集文章信息
class NextNotesInfo {
  /// 文章ID
  int notesId;

  /// 文章标题
  String title;

  /// 封面图片
  String coverPicture;

  /// 点赞数
  int notesLikeNum;

  /// 收藏数
  int notesCollectionNum;

  /// 续写层级
  int continuationLevel;

  /// 文章类型（0=图文，1=视频）
  int notesType;

  NextNotesInfo({
    required this.notesId,
    required this.title,
    required this.coverPicture,
    required this.notesLikeNum,
    required this.notesCollectionNum,
    required this.continuationLevel,
    this.notesType = 0, // 默认为图文类型
  });

  factory NextNotesInfo.fromJson(Map<String, dynamic> json) {
    return NextNotesInfo(
      notesId: _toInt(json['notesId']),
      title: _toString(json['title']),
      coverPicture: _toString(json['coverPicture']),
      notesLikeNum: _toInt(json['notesLikeNum']),
      notesCollectionNum: _toInt(json['notesCollectionNum']),
      continuationLevel: _toInt(json['continuationLevel']),
      notesType: _toInt(json['notesType']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'notesId': notesId,
      'title': title,
      'coverPicture': coverPicture,
      'notesLikeNum': notesLikeNum,
      'notesCollectionNum': notesCollectionNum,
      'continuationLevel': continuationLevel,
      'notesType': notesType,
    };
  }
}

